/**
 * 
 */
package edu.gtcfla.km.admin.appmodules.service.impl;

import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.vsg.biz.BusinessEntityFactory;
import org.vsg.biz.BusinessOperationException;
import org.vsg.biz.account.ModuleFunction;
import org.vsg.biz.account.ModuleFunctoinBindingAction;
import org.vsg.biz.account.Permission;
import org.vsg.biz.account.Role;
import org.vsg.biz.account.repositories.BindingActionRepo;
import org.vsg.biz.account.repositories.MenuRepo;
import org.vsg.biz.account.repositories.ModuleFunctionBindingActRepo;
import org.vsg.biz.account.repositories.ModuleFunctionRepo;
import org.vsg.biz.account.repositories.PermissionRepo;
import org.vsg.biz.account.repositories.ResourceRepo;
import org.vsg.biz.account.repositories.RoleRepo;
import org.vsg.biz.account.repositories.UrlRepo;
import org.vsg.common.web.json.SelectItem;

import edu.gtcfla.km.admin.appmodules.service.FunctionManagerService;
import edu.gtcfla.km.admin.appmodules.vo.ModuleFunctionPermissionVO;
import edu.gtcfla.km.admin.appmodules.vo.ModuleFunctionVO;

/**
 * @author Bill Vison
 *
 */

public class ModuleFunctionServiceI implements FunctionManagerService {

	@Resource
	private ModuleFunctionRepo moduleFunctionRepo;
	
	@Resource
	private MenuRepo menuRepo;
	
	@Resource
	private UrlRepo urlRepo;
	
	@Resource
	private RoleRepo roleRepo;
	
	@Resource
	private PermissionRepo permissionRepo;

	@Resource
	private ResourceRepo resourceRepo;
	
	@Autowired
	private BindingActionRepo bindingActionRepo;
	
	@Autowired
	private ModuleFunctionBindingActRepo moduleFunctionBindingActRepo;
	
	private static BusinessEntityFactory entityFactory = BusinessEntityFactory.getInstance();		

	@Override
	public void update(ModuleFunctionVO vo) throws BusinessOperationException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Set<ModuleFunctionPermissionVO> listPermissionFunsByRoleId(String roleId,
			String modResId) throws BusinessOperationException {

		// --- find all available permission ---
		Iterable<ModuleFunction> funIter = moduleFunctionRepo.findAllAvailableByModuleId(modResId);
		
		Map<String, Permission> permMap = permissionRepo.findModuleFunctionPermissionMapByRoleId(roleId, modResId);

		// --- show module iter ---
		Set<ModuleFunctionPermissionVO> result = new LinkedHashSet<ModuleFunctionPermissionVO>();

		for (ModuleFunction fun : funIter) {
			ModuleFunctionPermissionVO mfpvo = new ModuleFunctionPermissionVO(fun);
			
			Permission perm = permMap.get(mfpvo.getId());
			if (perm != null) {
				String bindingActionId = perm.getBindingActionId();
				mfpvo.setAcl( bindingActionId );
			} else {
				mfpvo.setAcl("");
			}
			
			Set<SelectItem> actionEvents = new LinkedHashSet<SelectItem>();
			
			Set<ModuleFunctoinBindingAction> funActs =   moduleFunctionBindingActRepo.findAllByFunctionId(fun.getId());
			for (ModuleFunctoinBindingAction act : funActs) {
				SelectItem selItem = new SelectItem();
				
				selItem.setId( act.getBindingActId() );
				selItem.setName( act.getBindingActName() );
				
				actionEvents.add( selItem );
			}
			mfpvo.setActionEvents( actionEvents );


			
			result.add(mfpvo);
		}

		return result;
	}

	@Override
	public void authFunsWithUnderRes(String roleId, String modResId,
			Set<ModuleFunctionPermissionVO> newPerms) throws BusinessOperationException {
		
		Role role = this.roleRepo.findOne( roleId );
		
		Set<Permission> permSet = new LinkedHashSet<Permission>();
		
		for (ModuleFunctionPermissionVO permItem : newPerms ) {
			
			org.vsg.biz.account.Resource res = resourceRepo.findOne( permItem.getId() );
			
			Permission perm = entityFactory.create( Permission.class );
			perm.setBindingActionId( permItem.getAcl() );
			perm.setAssignedRole( role );
			perm.setAssignedResource( res );
			permSet.add( perm );
		}
		
		permissionRepo.removePermissionForFunsByModule(roleId, modResId);
		
		// --- add new permission ---
		permissionRepo.saveAll( permSet );		
	}

	@Override
	public void update(ModuleFunctionVO vo, String[] ignoreProperties)
			throws BusinessOperationException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateAllFunctions(Set<ModuleFunctionVO> voSet)
			throws BusinessOperationException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateAllFunctions(Set<ModuleFunctionVO> voSet,
			String[] ignoreProperties) throws BusinessOperationException {
		// TODO Auto-generated method stub
		
	}	
		
	


}
